/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ulss.deploybackup.commons;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import cn.ac.iie.ulss.deploybackup.config.Configuration;
import cn.ac.iie.ulss.deploybackup.config.ConfigurationException;
import cn.ac.iie.ulss.deploybackup.util.Meta;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String SHEETMETAMAP = "sheetMetaMap";
    public static final String STOREPATH = "storePath";
    public static final String METASIZE = "metaSize";
    public static final String META = "meta";
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    //logger
    static Logger logger = null;

    static {
        PropertyConfigurator.configure("log4j.properties");
        logger = Logger.getLogger(RuntimeEnv.class.getName());
    }

    public static boolean initialize(Configuration pConf) throws ConfigurationException {

        if (pConf == null) {
            logger.error("configuration object is null");
            return false;
        }
        conf = pConf;

        String storePath = conf.getString(STOREPATH, "");
        if (storePath.isEmpty()) {
            logger.error("parameter jettyServerIP does not exist or is not defined");
            return false;
        }
        dynamicParams.put(STOREPATH, storePath);

        int metaSize = conf.getInt(METASIZE, 0);
        if (metaSize == 0) {
            logger.error("parameter metaSize is 0.It dose not mean anything");
            return false;
        }
        dynamicParams.put(METASIZE, metaSize);

        Map<String, Meta> sheetMetaMap = new HashMap<String, Meta>();
        for (int i = 1; i <= metaSize; i++) {
            String confName = META.concat(String.valueOf(i));
            String tempConf = conf.getString(confName, "");
            String[] confs = tempConf.split("[|]");
            if (confs.length != 5) {
                logger.error("parameter " + confName + " is wrong, and the parameter is " + tempConf);
                return false;
            }
            sheetMetaMap.put(confs[0], new Meta(confs[1], confs[2], confs[3], confs[4]));
        }
        dynamicParams.put(SHEETMETAMAP, sheetMetaMap);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
